package command.webServices
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import event.WebServiceEvent;
	
	import flash.events.Event;
	
	import model.Model;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	/**
	 * Methos to handle : api.wunderground.com
	 */ 
	public class GetWeatherCommand implements ICommand, IResponder
	{
		
		private var model:Model = Model.instance;
		
		public function GetWeatherCommand()
		{
		}
		
		public function execute(evento:CairngormEvent):void
		{
			var service:HTTPService = new HTTPService();
			var location:String;
			service.method = "GET";
			service.resultFormat="e4x";
			if(evento.data.lat!=null)
				location= evento.data.lat + "," + evento.data.lng 
				
			//service.url = "http://api.wunderground.com/auto/wui/geo//WXCurrentObXML/index.xml?query=52.167194,4.608765";
			service.url = "http://api.wunderground.com/auto/wui/geo//WXCurrentObXML/index.xml?query="+location;
			service.addEventListener(ResultEvent.RESULT, result);
			service.addEventListener(FaultEvent.FAULT, fault);
			service.send();
			
		}
		
		public function result(data:Object):void
		{
			
			var resultXML    : XML = XML(data.result);
			
			
			//var resul_xml_list:XMLList = resultXML..observation_location;			
			//Model.instance.weather_xml = resul_xml_list;
			Model.instance.weather_xml = resultXML;
			//trace("Weather-xml-list" + Model.instance.weather_xml);
			
			/*var response : CairngormEvent = new CairngormEvent(WebServiceEvent.WEATHER_COMPLETE);
			response.data = resul_xml_list;
			CairngormEventDispatcher.getInstance().dispatchEvent( response );*/
			
			
		}
		
		public function fault(info:Object):void
		{
			Alert.show("Fault");
			
		}
	}
}